﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using GoodMan.Infrastructure.Entities.Enum;

namespace GoodMan.Infrastructure.Entities.Repository
{
    [Table("T_Refund")]
    public class T_Refund :IEntity<T_Refund>
    {
        [Key]
        [Column(TypeName = "int")]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        public DateTime ApplyDate { get; set; }

        [Column(TypeName = "varchar")]
        [StringLength(128)]
        public string ApplyUserName { get; set; }

        [Column(TypeName = "nvarchar")]
        [StringLength(512)]
        public string Remark { get; set; }
        public decimal Total { get; set; }
        public int Status { get; set; }
        public DateTime HandleDate { get; set; }

        [Column(TypeName = "varchar")]
        [StringLength(128)]
        public string HandleUserName { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(512)]
        public string HandleRemark { get; set; }

        public override T_Refund Find(string key)
        {
            int id = Int32.Parse(key);
            return Find(c => c.Id == id);
        }

        public override T_Refund Find(System.Linq.Expressions.Expression<Func<T_Refund, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Refund.FirstOrDefault(where);
                if (item != null)
                {
                    this.ApplyDate = item.ApplyDate;
                    this.ApplyUserName = item.ApplyUserName;
                    this.HandleDate = item.HandleDate;
                    this.HandleRemark = item.HandleRemark;
                    this.HandleUserName = item.HandleUserName;
                    this.Id = item.Id;
                    this.Remark = item.Remark;
                    this.Status = item.Status;
                    this.Total = item.Total;
                    return this;
                }
            }
            return null;
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<T_Refund, bool>> where)
        {
            using(Context ctx = new Context())
            {
                return ctx.Refund.Any(where);
            }
        }

        public override T_Refund FindLast(System.Linq.Expressions.Expression<Func<T_Refund, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Refund.OrderByDescending(c=>c.Id).FirstOrDefault(where);
                if (item != null)
                {
                    this.ApplyDate = item.ApplyDate;
                    this.ApplyUserName = item.ApplyUserName;
                    this.HandleDate = item.HandleDate;
                    this.HandleRemark = item.HandleRemark;
                    this.HandleUserName = item.HandleUserName;
                    this.Id = item.Id;
                    this.Remark = item.Remark;
                    this.Status = item.Status;
                    this.Total = item.Total;
                    return this;
                }
            }
            return null;
        }
    }
}
